﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
//using System.Web.Routing;
using System.Globalization;
using System.Web.Routing;

namespace MVCCentral.Framework
{
    public static class HtmlHelpers
    {

        public static string ListActionLinks(this HtmlHelper helper, string list, string controllerName, string linkType)
        {
            string result = string.Empty;
            if (string.IsNullOrEmpty(list)) return list;

            StringBuilder sb = new StringBuilder();
            string[] tags = list.Split(',');

            foreach (string tag in tags)
            {
                result += "<a href=\"/" + controllerName + "/" + linkType + "/" + tag.Trim().ToSEOString() + "\">" + tag.Trim() + "</a>, ";
            }
            
            if (result != string.Empty)
                result = result.Substring(0, (result.Length) - 2);

            return result;
        }

        public static string ReadMoreLink(this HtmlHelper helper, string controller, string action)
        {
            string result = string.Empty;
            result += "<a href=\"/" + controller + "/" + action + "/\">read more...</a>";
            return result;
        }

        public static string ReadMoreLink(this HtmlHelper helper, string controller, string action, object htmlAttributes)
        {
            string result = string.Empty;
            result += "<a href=\"/" + controller + "/" + action + "/" + RenderHtmlAttributes(htmlAttributes) + "\">read more &gt;&gt;</a>";
            return result;
        }

        private static string RenderHtmlAttributes(object htmlAttr)
        {
            IDictionary<string, object> attributes = new RouteValueDictionary(htmlAttr);

            StringBuilder sb = new StringBuilder();
            string result = string.Empty;
            if (attributes != null)
            {
                foreach (var entry in attributes)
                {
                    string value = Convert.ToString(entry.Value, CultureInfo.InvariantCulture);
                    sb.Append(value + "/");
                }
            }
            if (sb.ToString() != string.Empty)
                result = sb.ToString().Substring(0, sb.ToString().Length - 1);

            return result;
        }
    }
}
